define(function (require, exports, module) {
    var $ = require('jquery');
    var jquery = require('jquery');
    var _ = require('underscore');
    var AjaxManager = require('./ajaxManager').AjaxManager;
    var Mustache = require('Mustache');

    function DeliveryBlacklist ($container) {
        var that = this;

        this.$container = $container;

        this.$editor = this.$container.find('textarea');

        this.$table = this.$container.find('table');

        DeliveryBlacklist.queryBuyers(function (buyers) {
            that.render(DeliveryBlacklist.getBuyerArr(buyers));
        });

        this.$editor.change(function () {
            // todo

        });

        this.$container.find('.btn-add').click(function (ev) {
            var buyerArr = DeliveryBlacklist.getBuyerArr(that.$editor.val());

            ev.preventDefault();

            if (buyerArr.length > 0) {
                DeliveryBlacklist.addBuyers(buyerArr, function () {
                    DeliveryBlacklist.queryBuyers(function (buyers) {
                        that.render(DeliveryBlacklist.getBuyerArr(buyers));
                    });
                });
            } else {
                // todo
                alert('买家id填写有误！');
            }
        });
    }

    DeliveryBlacklist.prototype.render = function (buyerArr) {
        var blankArr = [];
        var $tbody = this.$table.find('tbody');

        if (buyerArr.length < 14) {
            while (blankArr.length + buyerArr.length < 14) {
                blankArr.push('');
            }
        }

        $tbody.html(((buyerArr.length > 0) ? Mustache.render($('#delivery-blacklist-list-tpl-1').html(), {
            buyers: buyerArr
        }) : '')
            + ((blankArr.length > 0) ? Mustache.render($('#delivery-blacklist-list-tpl-2').html(), {
            buyers: blankArr
        }) : ''));

        $tbody.find('tr').each(function (index) {
            if (index % 2 == 1) {
                $(this).addClass('even');
            }
        });

        $tbody.delegate('.act a', 'click', function (ev) {
            var $this = $(this);

            ev.preventDefault();

            DeliveryBlacklist.deleteBuyer($this.attr('data-id'), function () {
                $this.parents('tr').hide();
            });
        });
    };

    DeliveryBlacklist.getBuyerArr = function (str) {
        var arr0 = str.split(';');
        var arr1 = [];

        $.each(arr0, function (index, value) {
            if (value) {
                arr1.push(value);
            }
        });

        return arr1;
    };

    DeliveryBlacklist.validate = function (str) {
        return /^;?([^,;]+(;[^,;]+)*);?$/.test(str);
    };

    DeliveryBlacklist.queryBuyers = function (onSuccess) {
        AjaxManager.send('queryNoSendBuyer', {
            userId: $(document.body).attr('data-userid')
        }, function (result) {
            onSuccess && onSuccess(result.buyers);
        });

    };

    DeliveryBlacklist.addBuyers = function (buyerArr, onSuccess) {
        AjaxManager.send('addNoSendBuyer', {
            buyers: encodeURI(buyerArr.join(';')),
            userId: $(document.body).attr('data-userid')
        }, function (result) {
            if (result.success) {
                onSuccess && onSuccess();
            }
        });
    };

    DeliveryBlacklist.deleteBuyer = function (buyer, onSuccess) {
        AjaxManager.send('delNoSendBuyer', {
            buyer: buyer,
            userId: $(document.body).attr('data-userid')
        }, function (result) {
            if (result.success) {
                onSuccess && onSuccess();
            }
        });
    };

    exports.DeliveryBlacklist = DeliveryBlacklist;
});